<?php


namespace app\api\middleware;


use app\api\service\JwtService;
use think\Exception;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\Response;


class Check
{

    /**
     * @param $request
     * @param \Closure $next
     * @return mixed
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:
     */
    public function handle($request, \Closure $next)
    {
        $appid = $request->param('appId');
        $erpSign = $request->param('sign');
        $time = $request->param('timestamp');
        try {
            if (!empty($appid) && !empty($erpSign) && !empty($time)) {
                $sign = $this->encrypt($appid . '_' . $time . '_haiding');
                if ($sign != str_replace(" ", "+", $erpSign)) {
                    return $this->result([], 400, '签名错误');
                }
//                $allip = [
//                    '127.0.0.1'
//                ];
//                $ip =  $this->getRealIp();
//                if(!in_array($ip,$allip)){
//                    return $this->result([],"400",$ip.'IP未在白名单');
//                }
                return $next($request);
            } else {
                return $this->result([], 400, 'appId或sign和timestamp不能为空');
            }
        } catch (\Exception $exception) {
            return $this->result([], 400, $exception->getMessage());
        }

    }

    /**
     * 解密
     * @param String input 解密的字符串
     * @param String key   解密的key
     * @return String
     */
    function decrypt($sStr)
    {
        $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', '', OPENSSL_RAW_DATA);
        return $decrypted;
    }

    /**
     * 加密
     * @param String input 加密的字符串
     * @param String key   解密的key
     * @return HexString
     */
    public function encrypt($input)
    {
        $data = openssl_encrypt($input, 'AES-128-ECB', 'mianbeiaehaiding', OPENSSL_RAW_DATA);
        $data = base64_encode($data);
        return $data;
    }

    function getRealIp()
    {
        $ip = false;
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) {
                array_unshift($ips, $ip);
                $ip = FALSE;
            }
            for ($i = 0; $i < count($ips); $i++) {
                if (!preg_match("^(10│172.16│192.168).", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }

    /**
     * @param $data
     * @param int $code
     * @param string $msg
     * @return \think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:返回封装后的API数据到客户端
     */
    protected function result($data, int $code = 0, $msg = '')
    {
        $result = [
            'code' => $code,
            'msg' => $msg,
            'time' => time(),
            'data' => $data,
        ];
        return json($result);
    }

}